package cn.jingyuan.swan.uaa.service.impl;

import cn.jingyuan.swan.cloud.core.Result;
import cn.jingyuan.swan.cloud.oauth2.DefaultAuthority;
import cn.jingyuan.swan.cloud.oauth2.DefaultUserDetails;
import cn.jingyuan.swan.cloud.oauth2.client.DefaultOAuth2ClientDetails;
import cn.jingyuan.swan.cloud.oauth2.client.DefaultOAuth2ClientProperties;
import cn.jingyuan.swan.system.enums.SysAccStatusEnum;
import cn.jingyuan.swan.system.model.UserAccount;
import cn.jingyuan.swan.uaa.helper.SysAuthorityHelper;
import cn.jingyuan.swan.uaa.service.feign.UserService;
import com.google.common.collect.Maps;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserService sysUserService;

    @Resource
    DefaultOAuth2ClientProperties auth2ClientProperties;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // ---------------- 模拟实现 ----------------

        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户不存在");
        }

        DefaultUserDetails uds = new DefaultUserDetails();
        uds.setAuthorities(Collections.emptyList());
        uds.setUsername("admin");
        uds.setPassword("swan");
        uds.setAccountNonExpired(true);
        uds.setAccountNonLocked(true);
        uds.setCredentialsNonExpired(true);
        uds.setEnabled(true);
        uds.setUserId(111111L);
        uds.setClientId("");
        uds.setNickName("管理员");
        uds.setAvatar("图像");
        uds.setAccountId(2222220L);
        uds.setAccountType("jiukang");
        uds.setAttrs(Maps.newHashMap());

        return uds;

    }

    private UserDetails load(String username) {
        // ---------------- 正式实现 ----------------
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        Result<UserAccount> userAccountResult = sysUserService.userLogin(params);
        UserAccount userAccount = userAccountResult.getData();
        if (null == userAccount) {
            throw new UsernameNotFoundException("用户不存在");
        }

        DefaultOAuth2ClientDetails clientDetails = auth2ClientProperties.getProvider().get("swan");
        SysAccStatusEnum accountStatus = userAccount.getAccStatus();

        boolean accountNonLocked = accountStatus != SysAccStatusEnum.LOCKED;
        boolean credentialsNonExpired = true;
        boolean enabled = accountStatus == SysAccStatusEnum.ENABLED;
        boolean accountNonExpired = true;


        Collection<DefaultAuthority> defaultAuthorities = SysAuthorityHelper.to(userAccount.getAuthorities());

        DefaultUserDetails userDetails = new DefaultUserDetails();
        userDetails.setUserId(userAccount.getUserId());
        userDetails.setUsername(userAccount.getAccount());
        userDetails.setPassword(userAccount.getPassword());
        userDetails.setAuthorities(defaultAuthorities);
        userDetails.setAccountNonLocked(accountNonLocked);
        userDetails.setAccountNonExpired(credentialsNonExpired);
        userDetails.setEnabled(enabled);
        userDetails.setCredentialsNonExpired(accountNonExpired);
        userDetails.setClientId(clientDetails.getClientId());
        userDetails.setNickName(userAccount.getNickname());
        userDetails.setAvatar(userAccount.getAvatar());
        userDetails.setAccountId(userAccount.getAccountId());
        userDetails.setAccountType(userAccount.getAccountType().getValue());

        return userDetails;

    }

}
